/**
 * Project Name:client_150825
 * File Name:RegisterRecordFilter.java
 * Package Name:com.eloancn.dkweb.filter
 * Date:2015-9-10下午01:44:25
 * Copyright (c) 2015, eloancn All Rights Reserved.
 *
*/

package xin.petstore.framework.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.eloancn.common.constant.Constant;
import com.eloancn.common.utils.CommonUtils;

/**   
* @Title: RegisterRecordFilter.java 
* @Package com.eloancn.common.filter 
* @Description: 推广统计
* @author zsongbo 
* @date 2016年5月20日 下午4:43:35 
* @version V1.0   
*/
public class RegisterRecordFilter  implements Filter{

	/* 
	 * 销毁
	 */
	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException , ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		String refer = httpRequest.getHeader("Referer");
		if ((refer!=null) && (!refer.contains(Constant.COOKIE_DOMAIN))){
			CommonUtils.addCookie(httpResponse, Constant.COOKIE_REFERER, refer);
		}
		StringBuffer strBuff = new StringBuffer();
		//判断是否包含推广参数
		String existParam = CommonUtils.getExistParam(httpRequest.getParameterNames(), Constant.SIGN_ARRAY);
		if(null != existParam){
			String channel=httpRequest.getParameter(existParam);//获取渠道
			String act_sign=httpRequest.getParameter(Constant.PARAM_ACT);//获取活动
			String pkey=httpRequest.getParameter(Constant.PARAM_PKEY);//获取哪里过来的
        	strBuff.setLength(0);
        	strBuff.append(httpRequest.getRequestURL().toString()).append("?").append(httpRequest.getQueryString());
        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_KEY_RECORD,strBuff.toString());
        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_ACT_SIGN,act_sign);
        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_CH_SIGN,channel);
        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_PKEY,pkey);
		}
		//特殊处理hao123推广链接
		if(StringUtils.isNotBlank(refer)){
			for (int i = 0; i < Constant.SIGN_ARRAY_TWO_DIMENSION.length; i++) {
				String[] strings = Constant.SIGN_ARRAY_TWO_DIMENSION[i];
				for (int j = 0; j < strings.length; j++) {
					if (refer.contains( strings[0])) {
						strBuff.setLength(0);
						strBuff.append(Constant.HAO123_URL).append("/?").append(Constant.HAO123_SIGN);
						CommonUtils.addCookie(httpResponse, Constant.COOKIE_KEY_RECORD, strBuff.toString());
						CommonUtils.addCookie(httpResponse, Constant.COOKIE_REFERER, refer);
						String act_sign=httpRequest.getParameter(Constant.PARAM_ACT);//获取活动
						String pkey=httpRequest.getParameter(Constant.COOKIE_PKEY);//获取哪里过来的
			        	strBuff.setLength(0);
			        	strBuff.append(httpRequest.getRequestURL().toString()).append("?").append(httpRequest.getQueryString());
			        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_KEY_RECORD,strBuff.toString());
			        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_ACT_SIGN,act_sign);
			        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_CH_SIGN,"HAO123KZ_SIGN");
			        	CommonUtils.addCookie(httpResponse, Constant.COOKIE_PKEY,pkey);
						break;
					}
				}
			}
		}
		strBuff.setLength(0);//清空
		chain.doFilter(request, response);
	}

	/* 
	 * 初始化参数
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {}
	
}

